package EVGENIA;

import java.io.*;
import java.io.IOException;

public class Logfunc {
  public Logfunc() {
  }

  public void save(String fileName) {
    /* Maya's implementation*/
  }

  public void load(String fileName) {
    /* Maya's implementation*/
  }

  /**
   *
   * <p>Title: FireWall</p>
   *
   * <p>Description: Gets the right turnOnLogger string accordingly to the
   * rule type of the recieved rule line
   *
   * <p>Copyright: Copyright (c) 2006</p>
   *
   * @param p_line String - rule line
   * @author Evgenya Feldman
   * @version 1.0
   */
private String logOnString(String p_line)
{
    String ipRule = new String("IpRule");
    String matchRule = new String ("MatchRule");
    String matchLogOn = new String("MatchLogOn");
    String ipLogOn = new String("IpLogOn");

    if(p_line.indexOf(ipRule) != -1)
    {
      return ipLogOn;
    }
    if (p_line.indexOf(matchRule) != -1)
    {
      return matchLogOn;
    }
    return "";

}
/**
 *
 * <p>Title: FireWall</p>
 *
 * <p>Description: Function called on shange to the logger status.
 * 1. Saves the previous rules to a given file
 * 2. Updates the file accordingly to the logger new state
 * 3. Loads the file back to the FireWall
 *
 * <p>Copyright: Copyright (c) 2006</p>
 *
 * @param p_rulesFileName String - path of the file to which the rules
 *                                 will be saved and later loaded from
 * @param p_state boolean - requested state change
 *                          true - turn the logger ON
 *                          false - turn the logger OFF
 * @author Evgenya Feldman
 * @version 1.0
 */
  public void log(String p_rulesFileName, boolean p_state) throws IOException {

    File configFile = new File(
          "C:\\Documents and Settings\\Administrator\\Desktop\\log\\logStatus.txt");

    if ((p_state)&&(configFile.exists()))
    {
      /* The logger is turned On already*/
        return;
    }

    if ((!p_state)&&(!configFile.exists()))
    {
      /* The logger is turned OFF already*/
        return;
    }



    File srcFile = new File(p_rulesFileName);
    String updatedRulesFileName = new String(srcFile.getParent() + "\\NewRules.txt");
    File targetFile = new File(updatedRulesFileName); /*temp file*/
    FileInputStream fin = new FileInputStream(p_rulesFileName);
    FileOutputStream fout = new FileOutputStream(updatedRulesFileName);
    BufferedReader br = new BufferedReader(new InputStreamReader(fin));
    BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(fout));
    String strLine = new String(); /*read string*/
    String strUpdated = new String(); /*write string*/
    StringBuffer sbBuffer = new StringBuffer();
    int index;



    save(p_rulesFileName);

    if (targetFile.exists())
    {
      targetFile.delete();
    }

    targetFile.createNewFile();
    try
    {
      while ( (strLine = br.readLine()) != null)
      /* Go over the file*/
      {
        /*Must turn on the logger*/
        if (p_state)
        {
          if (strLine.indexOf(logOnString(strLine)) == -1)
          {
            strUpdated = strLine + " " + logOnString(strLine);
          }
          else {
            strUpdated = strLine;
          }
        }

        /*Must turn off the logger*/
        else
        {
          index = strLine.indexOf(logOnString(strLine));
          if (index != -1)
          {
            strUpdated = strLine.substring(0, index - 1);
          }
          else
          {
            strUpdated = strLine;
          }
        }
        bw.write(strUpdated);
        bw.newLine();
      }

      br.close();
      bw.close();
      fin.close();
      fout.close();
      srcFile.delete();
      targetFile.renameTo(srcFile);

      /* deal with the logStatus file which should
       save the current logger settings*/

      if (p_state)
      {
        /* Turn On logger - save the file*/
        if (!configFile.exists())
        {
          configFile.createNewFile();
        }
      }
      else
      {
        /* Turn Off logger - delete the file*/
        if (configFile.exists()) {
          configFile.delete();
        }
      }
    }

    catch(IOException except)
    {
      System.out.println(except);
    }

    load(p_rulesFileName);

    return;
  }
}
